import db from "../models/index.js";
import {Op} from "sequelize";
import {getNow} from "../utils/utils.js";

let bill = db.bill;
let billDao = {};

// 创建一个公司修改记录订单 记录之前的记录数据 is_complete 0未完成 1完成
billDao.createCompanyUpdateBill = async (category, remark, amount, creator_id, company_id, is_complete, from_id, create_time, bill_time, t) => {
  return bill.create({
    category, remark, amount, creator_id, is_complete, from_id, company_id, create_time, bill_time
  }, {transaction: t});
};

// 增加个人账单 is_complete 0未完成 1完成
billDao.createPersonalBill = async (category, remark, amount, creator_id, is_complete, bill_time, t, from_id = null) => {
  let create_time = getNow();
  return bill.create({
      category, remark, amount, creator_id, is_complete, from_id, company_id: null, create_time, bill_time
    }, {transaction: t}
  );
};

//  增加公司账单 is_complete 0未完成 1完成
billDao.createCompanyBill = async (category, remark, amount, creator_id, company_id, is_complete, bill_time, t, from_id = null) => {
  let create_time = getNow();
  return bill.create({
    category, remark, amount, creator_id, is_complete, from_id, company_id, create_time, bill_time
  }, {transaction: t});
};

// 删除账单
billDao.deleteBill = async (id, t) => {
  if (!t) {
    return bill.update({is_delete: 1}, {where: {[Op.or]: {id, from_id: id}}});
  }
  return bill.update({is_delete: 1}, {where: {[Op.or]: {id, from_id: id}}}, {transaction: t});
};

// 删除多个账单
billDao.deleteBills = async (ids, t) => {
  if (!t) {
    return bill.update({is_delete: 1}, {where: {[Op.or]: {id: {[Op.in]: ids, from_id: {[Op.in]: ids}}}}});
  }
  return bill.update({is_delete: 1}, {where: {[Op.or]: {id: {[Op.in]: ids, from_id: {[Op.in]: ids}}}}, transaction: t});
};

// 查询账单 带事务
billDao.getUserBills = async (creator_id, t) => {
  return bill.findAll({where: {creator_id, from_id: null, is_delete: 0}, transaction: t, raw: true});
};

// 查询用户私人账单 带事务
billDao.getUserPersonalBills = async (creator_id, t) => {
  return bill.findAll({where: {creator_id, from_id: null, company_id: null}, transaction: t, raw: true});
};

// 查询账单的所有修改记录 带事务锁
billDao.getBillHisById = async (id, t) => {
  return bill.findAll({
    where: {
      [Op.or]: {
        id, from_id: id
      }
    }, transaction: t, raw: true
  });
};

// 查询账单通过账单id
billDao.getBillById = async (id, t) => {
  return bill.findOne({where: {id}, transaction: t, raw: true});
};

// 查询账单的公司id
billDao.getBillCompanyId = async (id, t) => {
  return bill.findOne({where: {id}, transaction: t, raw: true});
};

// 获取账单的创建者
billDao.getCreator = async (id, t) => {
  return bill.findOne({where: {id}, attributes: ['creator_id'], transaction: t, raw: true});
};

// 查询用户删除的账单
billDao.getUserDeletedBills = async (creator_id, t) => {
  if (!t) {
    return bill.findAll({where: {creator_id, is_delete: 1, from_id: null}, raw: true});
  }
  return bill.findAll({where: {creator_id, is_delete: 1, from_id: null}, transaction: t, raw: true});
};

// 查询公司的账单
billDao.getCompanyBills = async (company_id, t) => {
  return bill.findAll({where: {company_id, from_id: null}, transaction: t, raw: true});
};

// 更新公司账单数据 is_complete 0未完成 1完成
billDao.updateCompanyBill = async (id, category, remark, amount, is_complete, create_time, creator_id, bill_time, t) => {
  return bill.update({category, remark, amount, is_complete, create_time, creator_id, bill_time}, {where: {id}, transaction: t});
};

// 更新个人订单数据 is_complete 0未完成 1完成
billDao.updatePersonalBill = async (id, category, remark, amount, is_complete, bill_time, t) => {
  return bill.update({category, remark, amount, is_complete, bill_time}, {where: {id}, transaction: t});
};

export default billDao;
